package part4createthread;

import java.util.concurrent.*;

/**
 * @description: 实现Callable接口，重现call()方法创建线程
 * @date: 2022/10/6 11:54.
 * @version: 1.0
 */
public class CreateThreadDemo03 implements Callable<Boolean> {
    private String url;
    private String name;

    public CreateThreadDemo03(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public Boolean call() throws Exception {
        WebDownLoader webDownLoader = new WebDownLoader();
        webDownLoader.download(url, name);
        System.out.println(Thread.currentThread().getName() + "下载了文件：" + name);
        return true;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        String url = "https://s1.ax1x.com/2022/06/19/XjT8pj.jpg";

        CreateThreadDemo03 t1 = new CreateThreadDemo03(url, "zh01.jpg");
        CreateThreadDemo03 t2 = new CreateThreadDemo03(url, "zh02.jpg");
        CreateThreadDemo03 t3 = new CreateThreadDemo03(url, "zh03.jpg");

        ExecutorService ser = Executors.newFixedThreadPool(3);
        Future<Boolean> r1 = ser.submit(t1);
        Future<Boolean> r2 = ser.submit(t2);
        Future<Boolean> r3 = ser.submit(t3);
        boolean rs1 = r1.get();
        boolean rs2 = r2.get();
        boolean rs3 = r3.get();
        System.out.println("rs1的下载结果:" + rs1);
        System.out.println("rs2的下载结果:" + rs2);
        System.out.println("rs3的下载结果:" + rs3);
        ser.shutdownNow();
    }

}
